using System;
using System.Web;
using System.Collections.Specialized;

namespace NET.Library.Web
{
	/// <summary>
    /// Used for web related items.
	/// </summary>
	public class WebItems : NET.Library.Types.Strings
	{
		

		/// '-----------------------------------------------------------------------------------------
		/// 'returns querystring values omitting invalid characters such as those used in script injection attacks
		/// '-----------------------------------------------------------------------------------------
		public static string QueryString(string QueryStringItem)
		{
			if (HttpContext.Current.Request.QueryString.Get(QueryStringItem)!=null &&
				HttpContext.Current.Request.QueryString.Get(QueryStringItem).Length != 0)
			{
				string strQueryString = RemoveInvalidChar(HttpContext.Current.Request.QueryString.Get(QueryStringItem));
				strQueryString = strQueryString.Replace("<", "");
				strQueryString = strQueryString.Replace(">", "");
				return strQueryString;
			}
			else
			{
				return String.Empty;
			}
		}

        /// '-----------------------------------------------------------------------------------------
        /// 'returns querystring value as integer omitting invalid characters such as those used in script injection attacks
        /// '-----------------------------------------------------------------------------------------
        public static int QueryStringInt(string queryStringItem)
        {
            if (HttpContext.Current.Request.QueryString.Get(queryStringItem) != null &&
                HttpContext.Current.Request.QueryString.Get(queryStringItem).Length != 0)
            {
                string strQueryString = RemoveInvalidChar(HttpContext.Current.Request.QueryString.Get(queryStringItem));
                strQueryString = strQueryString.Replace("<", "");
                strQueryString = strQueryString.Replace(">", "");
                return int.Parse(strQueryString);
            }
            else
            {
                return 0;
            }
        }
		/// '-----------------------------------------------------------------------------------------
		/// 'returns url with querystring 
		/// '-----------------------------------------------------------------------------------------
		public static string URL(string Page)
		{
			int intLoop1, intLoop2;
			string[] strArr1, strArr2; 
			string strURL;
			NameValueCollection colItems ;

			strURL = Page + "?";
			//' Load querystring variables into NameValueCollection variable
			colItems = HttpContext.Current.Request.QueryString;
			//' Get names of all keys into a string array
			strArr1 = colItems.AllKeys;
			for (intLoop1 = 0; intLoop1 < strArr1.GetUpperBound(0); intLoop1++)
			{
				if (intLoop1 == 0)
				{
					strURL = strURL + strArr1[intLoop1] + "=";
				}
				else
				{
					strURL = strURL + "&" + strArr1[intLoop1] + "=";
				}
				//' Get all values under this key.
				strArr2 = colItems.GetValues(intLoop1);
				for (intLoop2 = 0; intLoop2 < strArr2.GetUpperBound(0); intLoop2++)
				{
					strURL = strURL + strArr2[intLoop2];
				}					 // Next intLoop2
			}//Next intLoop1

			return strURL;
		}
		//		/// '-----------------------------------------------------------------------------------------
		//		/// 'returns a date from a SqlDataReader as a date formatted string if the date value is not null
		//		/// '-----------------------------------------------------------------------------------------
		//		public static string GetDBDatetimeText(SqlDataReader SqlDataReader, string FieldName)
		//		{
		//			SqlTypes.SqlDateTime sinField;
		//			DateTime dteDateValue;
		//
		//
		//			sinField = SqlDataReader.GetSqlDateTime(SqlDataReader.GetOrdinal(FieldName));
		//			if (sinField!=null)
		//			{
		//				dteDateValue = sinField.Value;
		//				return dteDateValue.ToShortDateString;
		//			}
		//
		//		}

	}
}
